//
//  UIDeviceExtension.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2024/1/8.
//

import Foundation
import UIKit

extension UIDevice {
    /// 顶部安全区高度
    static func ui_safeDistanceTop() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let window = windowScene.windows.first else {
                guard let statusBarManager = windowScene.statusBarManager else { return 0 }
                return statusBarManager.statusBarFrame.height
//在viewLoad或willappear时调用,即便prefersStatusBarHidden=true，xR亦为48，
//在didAppear之后调用，如果prefersStatusBarHidden=false为48，=true时不管刘海屏或灵动岛屏都为0
//有刘海的高度可能为44、47、48,灵动岛54
            }
            return window.safeAreaInsets.top
            //xr上safeAreaInsets(t=48,l=0,b=34,r=0)(prefersStatusBarHidden=true隐藏状态栏情况下亦是),
            //但前提是在didFinishLaunchingWithOptions里创建window
            //如果是在SenceDelegate里创建window,xr上safeAreaInsets(t=0,l=0,b=0,r=0)
        } else{
            guard let window = UIApplication.shared.windows.first else { return 0 }
            let _top = window.safeAreaInsets.top
            //iphone6.iOS12下：safeAreaInsets(t=20,l=0,b=0,r=0),
            return _top
        }
    }
    
//    /// 顶部状态栏高度（包括安全区）同上
//    static func ui_statusBarHeight() -> CGFloat {
////有刘海的高度可能为44、47、48,无刘海为20；有无刘海在prefersStatusBarHidden=true，且在didAppear之后调用都为0
//        var statusBarHeight: CGFloat = 0
//        if #available(iOS 13.0, *) {
//            let scene = UIApplication.shared.connectedScenes.first
//            guard let windowScene = scene as? UIWindowScene else { return 0 }
//            guard let statusBarManager = windowScene.statusBarManager else { return 0 }
//            statusBarHeight = statusBarManager.statusBarFrame.height
////在viewLoad或willappear时调用,即便prefersStatusBarHidden=true，xR亦为48，appear之后调用，如果prefersStatusBarHidden=false为48，=true则为0
//        } else {
//            statusBarHeight = UIApplication.shared.statusBarFrame.height
//            //iphone6.iOS12下=20，但prefersStatusBarHidden=true隐藏状态栏情况下=0
//        }
//        return statusBarHeight
//    }
    
    
    /// 底部安全区高度
    static func ui_safeDistanceBottom() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let window = windowScene.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        } else {//if #available(iOS 11.0, *)
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        }
//        return 0;
    }
    
   
    
//!!!!所以发现通过系统参数获取，情况太多，还不如能过机型来判断更可靠？
    
    /// 导航栏高度
    static func ui_navigationBarHeight() -> CGFloat {
        return 44.0
    }
    
    /// 状态栏+导航栏的高度
    static func ui_navigationFullHeight() -> CGFloat {
        return UIDevice.ui_safeDistanceTop() + UIDevice.ui_navigationBarHeight()
    }
    
    /// 底部导航栏高度
    static func ui_tabBarHeight() -> CGFloat {
        return 49.0
    }
    
    /// 底部导航栏高度（包括安全区）
    static func ui_tabBarFullHeight() -> CGFloat {
        return UIDevice.ui_tabBarHeight() + UIDevice.ui_safeDistanceBottom()
    }
}
